package com.zlb.app.modules.dict.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlb.app.modules.dict.entity.Dict;
import com.zlb.app.modules.dict.mapper.DictMapper;
import com.zlb.app.modules.dict.service.IDictService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Desc 字典表 业务实现层
 * @Author zWX1367238
 * @Date 2024-08-20 11:35:52
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    /**
     * 构建前端所需要树结构
     *
     * @param dicts 部门列表
     * @return 树结构列表
     */
    public static List<Dict> buildDictTree(List<Dict> dicts) {
        List<Dict> dictList = new ArrayList<>();
        List<Long> dictIdList = new ArrayList<>();
        for (Dict dict : dicts) {
            dictIdList.add(dict.getId());
        }
        for (Dict dict : dicts) {
            // 如果是顶级节点，遍历该父节点所有子节点
            if (!dictIdList.contains(dict.getParentId())) {
                recursionFn(dicts, dict);
                dictList.add(dict);
            }
        }
        if (dictList.isEmpty()) {
            dictList = dicts;
        }
        return dictList;
    }

    /**
     * 递归列表
     * 结束条件为所遍历的节点无下一级节点
     *
     * @param list 查询获得的所有部门数据
     * @param dict 顶级节点
     */
    private static void recursionFn(List<Dict> list, Dict dict) {
        // 得到子节点列表
        List<Dict> childList = getChildList(list, dict);
        dict.setChildList(childList);
        for (Dict tChild : childList) {
            // 如果子节点有下一级节点，得到下一级的节点列表
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 获得该节点的下一级子节点列表
     *
     * @param list 查询获得的所有部门数据
     * @param dict 顶级节点
     * @return 顶级节点的下一级子节点列表
     */
    private static List<Dict> getChildList(List<Dict> list, Dict dict) {
        List<Dict> dictList = new ArrayList<>();
        for (Dict d : list) {
            // 遍历非顶级节点，并获得传入参数顶级节点的下一级子节点列表
            if (d.getParentId() != null && d.getParentId().equals(dict.getId())) {
                dictList.add(d);
            }
        }
        return dictList;
    }

    /**
     * 判断是否有子节点
     *
     * @param list     节点列表
     * @param dictList 部门节点
     * @return Boolean
     */
    private static boolean hasChild(List<Dict> list, Dict dictList) {
        return getChildList(list, dictList).size() > 0;
    }

    @Override
    public List<Dict> getDictByType(String type) {
        return baseMapper.selectList(Wrappers.lambdaQuery(Dict.class)
                .eq(Dict::getDictType, type)
                .eq(Dict::getIsEnable, 0)
                .eq(Dict::getIsDeleted, 0)
                .orderByAsc(Dict::getDictSort));
    }

    @Override
    public List<Dict> getTreeDict() {
        List<Dict> dicts = baseMapper.selectList(Wrappers.lambdaQuery(Dict.class)
                .eq(Dict::getIsEnable, 0)
                .eq(Dict::getIsDeleted, 0)
                .orderByAsc(Dict::getParentId)
                .orderByAsc(Dict::getDictSort));
        return buildDictTree(dicts);
    }

}
